<?php
namespace app\zmt\controller;


use app\zmt\model\NavModel;
use app\zmt\validata\NavValidata;
use cmf\controller\AdminBaseController;

class NavController extends AdminBaseController
{
    //显示导航页面
    public function index()
    {
        //搜索
        $keyword = input('keyword/s','');
        $where = [];
        if($keyword){
            $where[] = ['title','like','%'.$keyword.'%'];
        }

        //查询分页排序
        $list = NavModel::order(['list_order'=>'DESC','id'=>'DESC'])
            ->paginate(5);
        $this->assign('list',$list);
        return $this->fetch('nav');

    }

    //添加页面
    public function add()
    {
        return $this->fetch();
    }

    //添加
    public function addPost()
    {
        //接收
        $data = input('post.');

        //图片处理器
        $data['image'] = cmf_get_image_url($data['image']);

        //验证器验证
        $validata = new NavValidata();
        $result = $validata->scene('add')->check($data);
        if(!$result){
            $this->error($validata->getError());
        }

        //添加
        $add = NavModel::create($data);
        if($add){
            $this->success('添加成功','Nav/index');
        } else {
            $this->error('添加失败');
        }
    }

    //编辑页面
    public function edit()
    {
        //接收id
        $id = input('id/d','');

        $list = NavModel::find($id);
        $this->assign('list',$list);
        return $this->fetch();

    }

    //编辑
    public function editPost()
    {
        //接收编辑用户信息
        $data = input('post.');

        //验证器验证
        $validata = new NavValidata();
        $result = $validata->scene('edit')->check($data);
        if(!$result){
            $this->error($validata->getError());
        }

        //图片获取器
        $data['image'] = cmf_get_image_url($data['image']);

        //验证数据是否为空
        $edit = NavModel::find($data['id']);
        if(!$edit){
            $this->error('未查询到数据');
        }

        //编辑
        $edit->title = $data['title'];
        $edit->image = $data['image'];
        $edit->url = $data['url'];
        if($edit->save()){
            $this->success('保存成功','Nav/index');
        }else{
            $this->error('保存失败');
        }

    }

    //删除
    public function delete()
    {
        //接收删除数据id
        $id = input('id/d','');
        //删除逻辑
        if(NavModel::destroy($id)){
            $this->success('删除成功','nav/index');
        }else{
            $this->error('删除失败');
        }
    }

    //排序
    public function px(){
        $nav = new NavModel();
        parent::listOrders($nav);
        $this->success('排序成功');
    }





















}